package com.bstek.service.impl;

import com.bstek.dao.MisDepartmentDao;
import com.bstek.model.domain.MisDepartment;
import com.bstek.service.MisDepartmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Transactional(rollbackFor = Exception.class)
@Service
public class MisDepartmentServiceImpl implements MisDepartmentService {


    @Inject
    MisDepartmentDao misDepartmentDao;


    @Override
    public  Map<String ,Object> getDept() {
        List<MisDepartment> ls =misDepartmentDao.getDepts();
        Map<String ,Object> map=new HashMap<String,Object> ();
        if(ls!= null && ls.size()>0){
                MisDepartment dept = ls.get(0);
                int parentId = dept.getId();
                map.put("id",parentId);
                map.put("label",dept.getName());
                map.put("parentId",dept.getParentId());
                List< Map<String,Object>> children= new ArrayList<>();
                Map<String,Object> listMap = null;
                List<MisDepartment> listChildren =misDepartmentDao.getDeptByParentId(parentId);
                for( MisDepartment department :listChildren){
                    listMap = new HashMap<>();
                    int childrenId=  department.getId();
                    String deptName= department.getName();
                    listMap.put("id",childrenId);
                    listMap.put("label",deptName);
                    listMap.put("parentId",department.getParentId());
                    children.add(listMap);
                }
                map.put("children",children);
            return map;
        }else{
            return null;
        }
    }

    @Override
    public int saveDepartment(MisDepartment misDepartment) {
        int num =misDepartmentDao.count();
        misDepartment.setCreateUser("cao");
        misDepartment.setId(num+100);
        return misDepartmentDao.saveDepartment(misDepartment);
    }

    @Override
    public int updateDepartment(MisDepartment misDepartment) {
        return misDepartmentDao.updateDepartment(misDepartment);
    }

    @Override
    public int delDepartmentById(Map<String, Object> map) {
        int id = (int) map.get("id");
        return misDepartmentDao.delDepartmentById(id);
    }


}
